package com.jh3502.helper.utils.listener;

import android.app.Activity;
import android.content.Context;
import android.text.method.KeyListener;
import android.view.View;
import android.widget.EditText;

import com.jh3502.helper.utils.eventbus.EventBusHelper;

import static com.jh3502.helper.base.BaseActivity.mSpeaker;

/**
 * Created by harisucici on 2018/4/14 0014.
 */

public class MyFocusChangeListener implements View.OnFocusChangeListener {
    private Activity context;
    private String text;
    private int index = -1;

    public MyFocusChangeListener(Context context, String text, int index) {
        this.context = (Activity) context;
        this.text = text;
        this.index = index;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        EditText editText = (EditText) v;
        EventBusHelper.sendEventBusMsg(EventBusHelper.PERSONNEW_INDEX_NORMAL, index + "");
        KeyListener keyListener = editText.getKeyListener();
        String s = editText.getText().toString().trim();
        if (s.length() == 0) {
            if (hasFocus) {
                mSpeaker.speeking(text);
            } else {
            }
        } else {
            if (keyListener == null) {
                if (hasFocus) {
                    mSpeaker.speeking(text + s);
                } else {
                }
            } else {
                if (hasFocus) {
                    mSpeaker.speeking(text);
                } else {
                    mSpeaker.speeking(text + s);
                }
            }
        }

    }

}
